﻿using System.Collections.Generic;
using System.Linq;
using Quartz;
using ZX.DBModule;


namespace ZX.BLL
{
    public class CommonDataCache : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            ZX.BLL.Extend.BaseBussnies bll = new Extend.BaseBussnies();
            bll.iTop = 500;
            //bll.entity = new V_CrmCustomer();
            //IList<V_CrmCustomer> list = bll.GetEntitys<V_CrmCustomer>("");
            //ZX.Common.WebCache.Remove("V_CrmCustomer");
            //ZX.Common.WebCache.Insert("V_CrmCustomer", list.ToList<BaseEntity>());
            //System.Web.HttpContext.Current.Cache.Insert("V_CrmCustomer", list.ToList<BaseEntity>());
            //System.Web.Caching.we
            //int i = (System.Web.HttpContext.Current.Cache.Get("V_CrmCustomer") as IList<BaseEntity>).Count;
        }
    }


    public class OutupData : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            //
            ZX.BLL.Extend.BaseBussnies bll = new Extend.BaseBussnies(new SysTable());
            IList<SysTable> list = bll.GetEntitys<SysTable>("TableName like 'Sys%' or TableName like 'Set%' or TableName like 'Dat%'");
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            foreach (var en in list)
            {
                BaseEntity be = ZX.Common.Reflection.CreateEntitysInstance(en.TableName) as BaseEntity;
                bll.entity = be;
                sb.Append(OutupSql(bll, be)).AppendLine().AppendLine().AppendLine().AppendLine();
            }
        }

        public  void Outup()
        {   
            //dic.asp
            ZX.BLL.Extend.BaseBussnies bll = new Extend.BaseBussnies(new SysTable());
            //IList<Entity> list = bll.GetEntitys<Entity>("EntityName like 'Sys%' or EntityName like 'Set%'");
            IList<SysTable> list = bll.GetEntitys<SysTable>("TableName like 'Sys%' or TableName like 'Set%'");
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            foreach (var en in list)
            {
                //DataTableBaseEntity 
                DataTableBaseEntity dbe = new DataTableBaseEntity();
                BaseEntity be = ZX.Common.Reflection.CreateEntitysInstance(en.TableName) as BaseEntity;
                if (be == null)
                    continue;
                bll.entity = be;
                sb.Append(OutupSql(bll, be)).AppendLine().AppendLine().AppendLine().AppendLine();
            }
            System.IO.File.AppendAllText("C:\\Sql.sql",sb.ToString(),System.Text.Encoding.UTF8);
        }
        //
        public  string OutupSql(ZX.BLL.Extend.BaseBussnies bll,BaseEntity en)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            bll.entity = en;
            IList<BaseEntity> list = bll.GetEntitys();
            if (list == null)
                return "";
            foreach (var e in list)
            {
                bll.entity = e;
                sb.Append( bll.GetSqlCommand(e)).AppendLine();
                //yeilt bll.GetSqlCommand();
            }
            return sb.ToString();
        }
    }
}

